<template>
  <view class="order-confirm-page">
    <!-- 头部信息 -->
    <view class="header-section">
      <image 
        :src="shopAvatar || '/static/default-shop.jpg'"
        class="shop-banner"
        mode="aspectFill"
      ></image>
      <text class="shop-title">{{ shopName }}</text>
    </view>

    <!-- 订单主体 -->
    <view class="main-content">
      <!-- 订单概览 -->
      <view class="order-overview">
        <text class="order-number">订单号：{{ orderId }}</text>
        <text class="order-date">下单时间：{{ orderTime }}</text>
      </view>

      <!-- 商品清单 -->
      <scroll-view class="product-list" scroll-y>
        <view 
          v-for="(item, index) in serviceItems"
          :key="index"
          class="product-item"
        >
          <image 
            :src="item.image || '/static/default-product.jpg'"
            class="product-image"
          ></image>
          <text class="product-name">{{ item.name }}</text>
          <text class="product-price">¥{{ item.price }}</text>
          <text class="product-quantity">×{{ item.quantity }}</text>
        </view>
      </scroll-view>

      <!-- 订单总价 -->
      <view class="total-section">
        <text class="subtotal">小计：¥{{ subtotal }}</text>
        <text class="discount">优惠券：-¥{{ discount }}</text>
        <text class="final-price">总计：¥{{ finalPrice }}</text>
      </view>
    </view>

    <!-- 底部操作栏 -->
    <view class="footer-actions">
      <button class="modify-btn" @click="openModifyModal">修改订单</button>
      <button class="pay-btn" @click="handlePayment">立即支付</button>
    </view>

    <!-- 修改订单弹窗 -->
    <uni-popup ref="modifyPopup" type="center">
      <view class="modify-dialog">
        <text class="modify-title">修改订单</text>
        <view class="modify-form">
          <!-- 新增车辆信息 -->
          <view class="form-item">
            <text class="item-label">车辆类型：</text>
            <picker 
              mode="selector" 
              :range="carTypeOptions"
              @change="handleCarTypeChange"
            >
              <view class="picker-box">
                {{ carType || '请选择车辆类型' }}
              </view>
            </picker>
          </view>
          <view class="form-item">
            <text class="item-label">车牌号码：</text>
            <input
              v-model="licensePlate"
              class="license-input"
              placeholder="请输入车牌号"
              maxlength="8"
            />
          </view>
          <!-- 原有服务选择 -->
          <view class="form-item">
            <text class="item-label">服务项目：</text>
            <picker 
              mode="selector"
              :range="serviceOptions"
              @change="handleServiceChange"
            >
              <view class="picker-box">
                {{ selectedService || '请选择服务项目' }}
              </view>
            </picker>
          </view>
          <view class="form-item">
            <text class="item-label">服务数量：</text>
            <input 
              type="number"
              v-model="quantity"
              class="quantity-input"
              placeholder="请输入数量"
            />
          </view>
        </view>
        <view class="modify-buttons">
          <button class="save-btn" @click="saveOrder">保存修改</button>
          <button class="cancel-btn" @click="closeModifyModal">取消</button>
        </view>
      </view>
    </uni-popup>
 
    <!-- 新增支付弹窗 -->
    <uni-popup ref="payPopup" type="center">
      <view class="pay-dialog">
        <text class="pay-title">支付确认</text>
        <view class="pay-content">
          <text class="amount-label">支付金额</text>
          <text class="amount">¥{{ finalPrice }}</text>
          <view class="payment-methods">
            <radio-group @change="handlePaymentMethodChange">
              <label class="method-item" v-for="method in paymentMethods" :key="method.value">
                <radio :value="method.value" :checked="method.value === selectedPayment"/>
                <image :src="method.icon" class="method-icon"></image>
                <text>{{ method.label }}</text>
              </label>
            </radio-group>
          </view>
        </view>
        <view class="pay-buttons">
          <button class="confirm-pay" @click="confirmPayment">立即支付</button>
          <button class="cancel-pay" @click="closePayModal">取消支付</button>
        </view>
      </view>
    </uni-popup>
  
  </view>
</template>

<script setup>
import { ref, computed } from 'vue';
import { onLoad } from '@dcloudio/uni-app';

// 响应式数据
const shopId = ref('');
const shopName = ref('未知店铺');
const shopAvatar = ref('');
const orderId = ref(Date.now().toString());
const orderTime = ref(new Date().toLocaleString());
const serviceItems = ref([]);
const subtotal = ref(0);
const discount = ref(0);
const modifyPopup = ref(null);
const serviceOptions = ref(['精洗汽车', '打蜡服务', '内饰清洁']);
const selectedService = ref('精洗汽车');
const quantity = ref(1);

// 新增响应式数据
const carType = ref('');
const licensePlate = ref('');
const carTypeOptions = ref(['轿车', 'SUV', 'MPV', '跑车', '新能源']);
const paymentMethods = ref([
  { label: '微信支付', value: 'wxpay', icon: '/static/icons/wechat-pay.png' },
  { label: '支付宝', value: 'alipay', icon: '/static/icons/alipay.png' }
]);
const selectedPayment = ref('wxpay');
const payPopup = ref(null);

// 计算属性
const finalPrice = computed(() => {
  return (subtotal.value - discount.value).toFixed(2);
});

// 修改保存订单方法（包含车辆信息）
const saveOrder = () => {
  serviceItems.value = [{
    id: 1,
    name: selectedService.value,
    price: 199,
    quantity: quantity.value,
    image: '/static/default-product.jpg',
    carType: carType.value,
    licensePlate: licensePlate.value
  }];
  calculateTotal();
  closeModifyModal();
};

// 新增支付方法（打开支付弹窗）
const handlePayment = () => {
  payPopup.value?.open();
};

const confirmPayment = () => {
  uni.showLoading({ title: '支付处理中' });
  setTimeout(() => {
    uni.hideLoading();
    payPopup.value?.close();
    uni.showToast({ title: '支付成功' });
    navigateToOrderSuccess();
  }, 2000);
};

const closePayModal = () => {
  payPopup.value?.close();
};

// 新增车辆类型选择
const handleCarTypeChange = (e) => {
  carType.value = carTypeOptions.value[e.detail.value];
};

// 支付方式选择
const handlePaymentMethodChange = (e) => {
  selectedPayment.value = e.detail.value;
};

// 弹窗操作
const openModifyModal = () => {
  modifyPopup.value?.open();
};

const closeModifyModal = () => {
  modifyPopup.value?.close();
};

// 服务选择
const handleServiceChange = (e) => {
  selectedService.value = serviceOptions.value[e.detail.value];
};

// 数据初始化
const initializeData = (options) => {
  if (options?.id) {
    shopId.value = options.id;
    const app = getApp();
    const foundShop = app.globalData.shopList?.find(
      shop => shop.id === Number(options.id)
    ) || {};
    
    shopName.value = foundShop.name || '未知店铺';
    shopAvatar.value = foundShop.avatar || '';
    serviceItems.value = [{
      id: 1,
      name: foundShop.service || '默认服务',
      price: foundShop.price || 199,
      quantity: 1,
      image: foundShop.serviceImage || ''
    }];
  }
};

// 计算总价
const calculateTotal = () => {
  subtotal.value = serviceItems.value.reduce(
    (sum, item) => sum + (item.price * item.quantity),
    0
  );
};

// 导航到成功页面
const navigateToOrderSuccess = () => {
  uni.navigateTo({
    url: '/pages/zhuanyexiche/WashCarInfo/OrderConfirm/order-success/order-success'
  });
};

// 生命周期钩子
onLoad((options) => {
  initializeData(options);
  calculateTotal();
});
</script>

<style>
.order-confirm-page {
  background-color: #f5f5f5;
  padding-bottom: 160rpx;
}

.header-section {
  position: relative;
  height: 420rpx;
}

.shop-banner {
  width: 100%;
  height: 100%;
  filter: brightness(0.9);
}

.shop-title {
  position: absolute;
  bottom: 40rpx;
  left: 32rpx;
  font-size: 44rpx;
  color: #fff;
  font-weight: 600;
  text-shadow: 2rpx 2rpx 6rpx rgba(0,0,0,0.3);
}

.main-content {
  margin-top: 40rpx;
  padding: 32rpx;
}

.order-overview {
  display: flex;
  justify-content: space-between;
  margin-bottom: 40rpx;
  background: #fff;
  border-radius: 16rpx;
  padding: 24rpx;
}

.order-number {
  color: #333;
  font-size: 32rpx;
}

.order-date {
  color: #666;
  font-size: 28rpx;
}

.product-list {
  height: 600rpx;
  overflow-y: scroll;
  background: #fff;
  border-radius: 16rpx;
  padding: 24rpx;
  margin: 24rpx 0;
}

.product-item {
  display: flex;
  margin-bottom: 32rpx;
  padding: 24rpx;
  border-radius: 12rpx;
  box-shadow: 0 4rpx 8rpx rgba(0,0,0,0.05);
}

.product-image {
  width: 120rpx;
  height: 120rpx;
  object-fit: cover;
  margin-right: 24rpx;
}

.product-name {
  color: #333;
  font-size: 30rpx;
  margin-bottom: 8rpx;
}

.product-price {
  color: #e74c3c;
  font-size: 30rpx;
  font-weight: 600;
}

.product-quantity {
  color: #999;
  font-size: 28rpx;
}

.total-section {
  display: flex;
  justify-content: space-between;
  margin-top: 40rpx;
  background: #fff;
  padding: 24rpx;
  border-radius: 16rpx;
}

.subtotal {
  color: #666;
  font-size: 28rpx;
}

.discount {
  color: #2ecc71;
  font-size: 28rpx;
}

.final-price {
  color: #e74c3c;
  font-size: 32rpx;
  font-weight: 600;
}

.footer-actions {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100rpx;
  background: #fff;
  display: flex;
  align-items: center;
  padding: 0 30rpx;
  box-shadow: 0 -2rpx 10rpx rgba(0,0,0,0.05);
}

.modify-btn {
  flex: 1;
  background: #f8f9fa;
  color: #333;
  border: 1rpx solid #eee;
  margin-right: 20rpx;
  transition: all 0.2s ease;
}

.modify-btn:active {
  opacity: 0.8;
}

.pay-btn {
  flex: 2;
  background: #07c160;
  color: #fff;
  transition: all 0.2s ease;
}

.pay-btn:active {
  opacity: 0.8;
}

/* 修改订单弹窗样式 */
.modify-dialog {
  background: #f8f9fa;
  width: 80%;
  border-radius: 24rpx;
  padding: 40rpx;
}

.modify-title {
  color: #2c3e50;
  font-size: 36rpx;
  font-weight: 600;
  margin-bottom: 30rpx;
  text-align: center;
}

.form-item {
  margin-bottom: 30rpx;
  background: #fff;
  border-radius: 12rpx;
  padding: 20rpx;
  display: flex;
  align-items: center;
}

.item-label {
  color: #666;
  font-size: 28rpx;
  min-width: 140rpx;
}

.picker-box, 
.license-input,
.quantity-input {
  flex: 1;
  color: #333;
  font-size: 28rpx;
  padding: 10rpx 0;
}

.license-input {
  border-bottom: 1rpx solid #eee;
  padding: 20rpx 0;
}

.modify-buttons {
  margin-top: 40rpx;
  display: flex;
  gap: 20rpx;
}

.save-btn {
  background: #07c160;
  color: white;
  flex: 1;
}

.cancel-btn {
  background: #f0f0f0;
  color: #666;
  flex: 1;
}

/* 支付弹窗样式 */
.pay-dialog {
  background: #fff;
  width: 80%;
  border-radius: 24rpx;
  padding: 40rpx;
}

.pay-title {
  font-size: 32rpx;
  color: #333;
  font-weight: 600;
  text-align: center;
  margin-bottom: 30rpx;
}

.pay-content {
  padding: 20rpx 0;
}

.amount-label {
  display: block;
  color: #999;
  text-align: center;
  font-size: 28rpx;
}

.amount {
  display: block;
  color: #e74c3c;
  font-size: 48rpx;
  text-align: center;
  font-weight: 600;
  margin: 20rpx 0;
}

.payment-methods {
  margin-top: 30rpx;
}

.method-item {
  display: flex;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1rpx solid #eee;
}

.method-icon {
  width: 50rpx;
  height: 50rpx;
  margin: 0 20rpx;
}

.pay-buttons {
  margin-top: 40rpx;
}

.confirm-pay {
  background: #07c160;
  color: white;
}

.cancel-pay {
  background: transparent;
  color: #666;
  margin-top: 20rpx;
}
</style>
